package com.shop.consumer;

import com.shop.dto.CoinOptionDto;
import com.shop.dto.MemberRegister;
import com.shop.dto.enums.CoinOptionType;
import com.shop.service.MemberCoinsService;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author wzby
 * @Date: 2022/11/15 10:17
 */
@Component
public class MemberRegisterConsumer {

    private static final String exchangeName = "Shop-MemberRegister-wzby";

    private static final String queueName = "memberRegister.register";
    @Autowired
    private MemberCoinsService memberCoinsService;


    @Bean
    //定义一个交换机
    public TopicExchange topicExchange() {
        return new TopicExchange(exchangeName, true, false);
    }

    @Bean
    //定义一个队列
    public Queue topicQueueA() {
        return QueueBuilder.durable(queueName).build();
    }



    @Bean
    //创建队列和交换机的绑定关系
    public Binding topicBindingA() {
        return BindingBuilder.bind(topicQueueA()).to(topicExchange()).with("#");
    }

    @RabbitHandler
    @RabbitListener(queues = queueName)
    public void processA(MemberRegister memberRegister) {
        System.out.println("消费者A收到:" + memberRegister.getEmail() + "," + memberRegister.getPhone());
        CoinOptionDto dto  = new CoinOptionDto();
        dto.setMemberId(memberRegister.getId());
        dto.setOptionQty(5);
        dto.setOptionType(CoinOptionType.REGISTER.getCode());
        dto.setCreateBy("系统");
        dto.setBrief("新用户注册赠送");
        dto.setRefId("");
        memberCoinsService.send(dto);
    }

}
